package com.thb.pay.manage.comm.shiro;


import com.google.common.collect.Maps;
import com.thb.pay.manage.comm.shiro.config.ShiroConstantConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;

import java.util.Iterator;
import java.util.Map;

@Slf4j
public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean {

    private ShiroConstantConfig shiroConstantConfig;

    public MyShiroFilterFactoryBean(ShiroConstantConfig shiroConstantConfig) {
        super.setLoginUrl(shiroConstantConfig.getLoginUrl());
        super.setSuccessUrl(shiroConstantConfig.getSuccessUrl());
        super.setUnauthorizedUrl(shiroConstantConfig.getUnauthorizedUrl());
        this.shiroConstantConfig = shiroConstantConfig;
    }

    public void setFilterChainDefinitionMap(Map<String, String> filterChainDefinitionMap) {
        Map<String, String> copyFilterChainMap = Maps.newLinkedHashMap();
        copyFilterChainMap.putAll(filterChainDefinitionMap);
        Iterator<String> urlIterator = copyFilterChainMap.keySet().iterator();
        while (urlIterator.hasNext()) {
            String urlStr = urlIterator.next();
            String filterGroups = filterChainDefinitionMap.remove(urlStr);
            String[] urlArr = urlStr.split(",");
            for (String url : urlArr) {
                if (StringUtils.isNotBlank(url))
                    filterChainDefinitionMap.put(url.trim(), filterGroups);
            }
        }
        super.setFilterChainDefinitionMap(filterChainDefinitionMap);
    }
}
